#include "Finish.h"
#include <stdlib.h>
#include <iostream>

using namespace std;

Symbol Finish::parse(ifstream &file, Symbol curToken) {
    curToken = matchToken(file, curToken, LBrace);

    while (curToken.t != RBrace) {
        if (curToken.t == Ambient) {
            curToken = matchToken(file, curToken, Ambient);
            if (curToken.isToken) {
                cout << "expected number, found " << tokenList[curToken.t]
                    << endl;
                exit(1);
            }

            ambient = curToken.num;
            curToken = nextToken(file);
        }

        if (curToken.t == Diffuse) {
            curToken = matchToken(file, curToken, Diffuse);
            if (curToken.isToken) {
                cout << "expected number, found " << tokenList[curToken.t]
                    << endl;
                exit(1);
            }

            diffuse = curToken.num;
            curToken = nextToken(file);
        }

        if (curToken.t == Specular) {
            curToken = matchToken(file, curToken, Specular);

            if (curToken.isToken) {
                cout << "expected number, found " << tokenList[curToken.t]
                    << endl;
                exit(1);
            }

            specular = curToken.num;
            curToken = nextToken(file);
        }
        
        if (curToken.t == Roughness) {
            curToken = matchToken(file, curToken, Roughness);

            if (curToken.isToken) {
                cout << "expected number, found " << tokenList[curToken.t]
                    << endl;
                exit(1);
            }

            roughness = curToken.num;
            curToken = nextToken(file);
        }

        if (curToken.t == Reflection) {
            curToken = matchToken(file, curToken, Reflection);

            if (curToken.isToken) {
                cout << "expected number, found " << tokenList[curToken.t]
                    << endl;
                exit(1);
            }

            reflection = curToken.num;
            curToken = nextToken(file);
        }

        if (curToken.t == Refraction) {
            curToken = matchToken(file, curToken, Refraction);

            if (curToken.isToken) {
                cout << "expected number, found " << tokenList[curToken.t]
                    << endl;
                exit(1);
            }

            refraction = curToken.num;
            curToken = nextToken(file);
        }


        if (curToken.t == IOR) {
            curToken = matchToken(file, curToken, IOR);

            if (curToken.isToken) {
                cout << "expected number, found " << tokenList[curToken.t]
                    << endl;
                exit(1);
            }

            ior = curToken.num;
            curToken = nextToken(file);
        }
    }

    return matchToken(file, curToken, RBrace);
}

